home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / gccdif~1.z / gccdif~1 / gcc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-30  |  55.7 KB  |  2,275 lines

  1. /* Compiler driver program that can handle many languages.
  2.    Copyright (C) 1987,1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. This paragraph is here to try to keep Sun CC from dying.
  21. The number of chars here seems crucial!!!!  */
  22.  
  23. void record_temp_file ();
  24.  
  25. /* This program is the user interface to the C compiler and possibly to
  26. other compilers.  It is used because compilation is a complicated procedure
  27. which involves running several programs and passing temporary files between
  28. them, forwarding the users switches to those programs selectively,
  29. and deleting the temporary files at the end.
  30.  
  31. CC recognizes how to compile each input file by suffixes in the file names.
  32. Once it knows which kind of compilation to perform, the procedure for
  33. compilation is specified by a string called a "spec".
  34.  
  35. Specs are strings containing lines, each of which (if not blank)
  36. is made up of a program name, and arguments separated by spaces.
  37. The program name must be exact and start from root, since no path
  38. is searched and it is unreliable to depend on the current working directory.
  39. Redirection of input or output is not supported; the subprograms must
  40. accept filenames saying what files to read and write.
  41.  
  42. In addition, the specs can contain %-sequences to substitute variable text
  43. or for conditional text.  Here is a table of all defined %-sequences.
  44. Note that spaces are not generated automatically around the results of
  45. expanding these sequences; therefore, you can concatenate them together
  46. or with constant text in a single argument.
  47.  
  48.  %%    substitute one % into the program name or argument.
  49.  %i     substitute the name of the input file being processed.
  50.  %b     substitute the basename of the input file being processed.
  51.     This is the substring up to (and not including) the last period.
  52.  %g     substitute the temporary-file-name-base.  This is a string chosen
  53.     once per compilation.  Different temporary file names are made by
  54.     concatenation of constant strings on the end, as in `%g.s'.
  55.     %g also has the same effect of %d.
  56.  %d    marks the argument containing or following the %d as a
  57.     temporary file name, so that that file will be deleted if CC exits
  58.     successfully.  Unlike %g, this contributes no text to the argument.
  59.  %w    marks the argument containing or following the %w as the
  60.     "output file" of this compilation.  This puts the argument
  61.     into the sequence of arguments that %o will substitute later.
  62.  %o    substitutes the names of all the output files, with spaces
  63.     automatically placed around them.  You should write spaces
  64.     around the %o as well or the results are undefined.
  65.     %o is for use in the specs for running the linker.
  66.     Input files whose names have no recognized suffix are not compiled
  67.     at all, but they are included among the output files, so they will
  68.     be linked.
  69.  %p    substitutes the standard macro predefinitions for the
  70.     current target machine.  Use this when running cpp.
  71.  %P    like %p, but puts `__' before and after the name of each macro.
  72.     This is for ANSI C.
  73.  %s     current argument is the name of a library or startup file of some sort.
  74.         Search for that file in a standard list of directories
  75.     and substitute the full pathname found.
  76.  %eSTR  Print STR as an error message.  STR is terminated by a newline.
  77.         Use this when inconsistent options are detected.
  78.  %a     process ASM_SPEC as a spec.
  79.         This allows config.h to specify part of the spec for running as.
  80.  %l     process LINK_SPEC as a spec.
  81.  %L     process LIB_SPEC as a spec.
  82.  %S     process STARTFILE_SPEC as a spec.  A capital S is actually used here.
  83.  %c    process SIGNED_CHAR_SPEC as a spec.
  84.  %C     process CPP_SPEC as a spec.  A capital C is actually used here.
  85.  %1    process CC1_SPEC as a spec.
  86.  %{S}   substitutes the -S switch, if that switch was given to CC.
  87.     If that switch was not specified, this substitutes nothing.
  88.     Here S is a metasyntactic variable.
  89.  %{S*}  substitutes all the switches specified to CC whose names start
  90.     with -S.  This is used for -o, -D, -I, etc; switches that take
  91.     arguments.  CC considers `-o foo' as being one switch whose
  92.     name starts with `o'.  %{o*} would substitute this text,
  93.     including the space; thus, two arguments would be generated.
  94.  %{S:X} substitutes X, but only if the -S switch was given to CC.
  95.  %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
  96.  %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
  97.  %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
  98.  
  99. The conditional text X in a %{S:X} or %{!S:X} construct may contain
  100. other nested % constructs or spaces, or even newlines.
  101. They are processed as usual, as described above.
  102.  
  103. The character | is used to indicate that a command should be piped to
  104. the following command, but only if -pipe is specified.
  105.  
  106. Note that it is built into CC which switches take arguments and which
  107. do not.  You might think it would be useful to generalize this to
  108. allow each compiler's spec to say which switches take arguments.  But
  109. this cannot be done in a consistent fashion.  CC cannot even decide
  110. which input files have been specified without knowing which switches
  111. take arguments, and it must know which input files to compile in order
  112. to tell which compilers to run.
  113.  
  114. CC also knows implicitly that arguments starting in `-l' are to
  115. be treated as compiler output files, and passed to the linker in their proper
  116. position among the other output files.
  117.  
  118. */
  119.  
  120. #ifdef CROSSATARI
  121. #ifdef atarist
  122. #undef atarist
  123. #endif
  124. #ifdef atariminix
  125. #undef atariminix
  126. #endif
  127. #endif
  128.   
  129. #include <stdio.h>
  130. #include <sys/types.h>
  131. #include <signal.h>
  132. #include <sys/file.h>
  133.  
  134. #include "config.h"
  135. #include "obstack.h"
  136. #include "gvarargs.h"
  137.  
  138. #if (defined(USG) || defined(atariminix))
  139. #define R_OK 4
  140. #define W_OK 2
  141. #define X_OK 1
  142. #define vfork fork
  143. #endif /* USG */
  144.  
  145. #ifdef atarist
  146. #include <osbind.h>
  147. #include <ctype.h>
  148. long _stksize = 8192;
  149. extern char *getenv();
  150. #endif
  151.  
  152. #define obstack_chunk_alloc xmalloc
  153. #define obstack_chunk_free free
  154. extern int xmalloc ();
  155. extern void free ();
  156.  
  157. /* If a stage of compilation returns an exit status >= 1,
  158.    compilation of that file ceases.  */
  159.  
  160. #define MIN_FATAL_STATUS 1
  161.  
  162. /* This is the obstack which we use to allocate many strings.  */
  163.  
  164. struct obstack obstack;
  165.  
  166. char *handle_braces ();
  167. char *save_string ();
  168. char *concat ();
  169. int do_spec ();
  170. int do_spec_1 ();
  171. char *find_file ();
  172. static char *find_exec_file ();
  173. void validate_switches ();
  174. void validate_all_switches ();
  175. void fancy_abort ();
  176.  
  177. /* config.h can define ASM_SPEC to provide extra args to the assembler
  178.    or extra switch-translations.  */
  179. #ifndef ASM_SPEC
  180. #define ASM_SPEC ""
  181. #endif
  182.  
  183. /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
  184.    or extra switch-translations.  */
  185. #ifndef CPP_SPEC
  186. #define CPP_SPEC ""
  187. #endif
  188.  
  189. /* config.h can define CC1_SPEC to provide extra args to cc1
  190.    or extra switch-translations.  */
  191. #ifndef CC1_SPEC
  192. #define CC1_SPEC ""
  193. #endif
  194.  
  195. /* config.h can define LINK_SPEC to provide extra args to the linker
  196.    or extra switch-translations.  */
  197. #ifndef LINK_SPEC
  198. #define LINK_SPEC ""
  199. #endif
  200.  
  201. /* config.h can define LIB_SPEC to override the default libraries.  */
  202. #ifndef LIB_SPEC
  203. #define LIB_SPEC "%{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
  204. #endif
  205.  
  206. /* config.h can define STARTFILE_SPEC to override the default crt0 files.  */
  207. #ifndef STARTFILE_SPEC
  208. #define STARTFILE_SPEC  \
  209.   "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
  210. #endif
  211.  
  212. /* This spec is used for telling cpp whether char is signed or not.  */
  213. #define SIGNED_CHAR_SPEC  \
  214.   (DEFAULT_SIGNED_CHAR ? "%{funsigned-char:-D__CHAR_UNSIGNED__}"    \
  215.    : "%{!fsigned-char:-D__CHAR_UNSIGNED__}")
  216.  
  217. /* This defines which switch letters take arguments.  */
  218.  
  219. #ifndef SWITCH_TAKES_ARG
  220. #define SWITCH_TAKES_ARG(CHAR)      \
  221.   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
  222.    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
  223.    || (CHAR) == 'I' || (CHAR) == 'Y' || (CHAR) == 'm' \
  224.    || (CHAR) == 'L' || (CHAR) == 'i' || (CHAR) == 'A')
  225. #endif
  226.  
  227. /* This defines which multi-letter switches take arguments.  */
  228.  
  229. #ifndef WORD_SWITCH_TAKES_ARG
  230. #define WORD_SWITCH_TAKES_ARG(STR) (!strcmp (STR, "Tdata"))
  231. #endif
  232.  
  233. /* This structure says how to run one compiler, and when to do so.  */
  234.  
  235. struct compiler
  236. {
  237.   char *suffix;            /* Use this compiler for input files
  238.                    whose names end in this suffix.  */
  239.   char *spec;            /* To use this compiler, pass this spec
  240.                    to do_spec.  */
  241. };
  242.  
  243. /* Here are the specs for compiling files with various known suffixes.
  244.    A file that does not end in any of these suffixes will be passed
  245.    unchanged to the loader and nothing else will be done to it.  */
  246.  
  247. struct compiler compilers[] =
  248. {
  249.   {".c",
  250.    "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{i*} %{trigraphs} -undef \
  251.         -D__GNUC__ %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!ansi:%p}\
  252.         %c %{O:-D__OPTIMIZE__} %{mshort:-D__MSHORT__} %{traditional}\
  253.         %{pedantic}\
  254.     %{Wcomment*} %{Wtrigraphs} %{Wall} %C\
  255.         %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%{o*}}%{M*:%{o*}} |\n\
  256.     %{!M*:%{!E:cc1 %{!pipe:%g.cpp} %1 \
  257.            %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{a}\
  258.            %{g} %{O} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\
  259.            %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
  260.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  261.            %{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  262.               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  263.               %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
  264.                       %{!pipe:%g.s}\n }}}"},
  265.   {".cc",
  266.    "cpp -+ %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{i*} \
  267.         -undef -D__GNUC__ %p\
  268.         %c %{O:-D__OPTIMIZE__} %{mshort:-D__MSHORT__} %{traditional}\
  269.         %{pedantic}\
  270.     %{Wcomment*} %{Wtrigraphs} %{Wall} %C\
  271.         %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%{o*}}%{M*:%{o*}} |\n\
  272.     %{!M*:%{!E:cc1plus %{!pipe:%g.cpp} %1\
  273.            %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{a}\
  274.            %{g} %{O} %{W*} %{w} %{pedantic} %{traditional}\
  275.            %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
  276.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  277.            %{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  278.               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  279.               %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
  280.                       %{!pipe:%g.s}\n }}}"},
  281.   {".i",
  282.    "cc1 %i %1 %{!Q:-quiet} %{Y*} %{d*} %{m*} %{f*} %{a}\
  283.     %{g} %{O} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\
  284.     %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
  285.     %{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  286.     %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  287.             %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %{!pipe:%g.s}\n }"},
  288.   {".s",
  289.    "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
  290.             %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %i\n }"},
  291.   {".S",
  292.    "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{trigraphs} \
  293.         -undef -D__GNUC__ -$ %p\
  294.         %c %{O:-D__OPTIMIZE__} %{mshort:-D__MSHORT__} %{traditional}\
  295.         %{pedantic}\
  296.     %{Wcomment*} %{Wtrigraphs} %{Wall} %C\
  297.         %i %{!M*:%{!E:%{!pipe:%g.s}}}%{E:%{o*}}%{M*:%{o*}} |\n\
  298.     %{!M*:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
  299.                     %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
  300.             %{!pipe:%g.s}\n }}}"},
  301.   /* Mark end of table */
  302.   {0, 0}
  303. };
  304.  
  305. /* Here is the spec for running the linker, after compiling all files.  */
  306.  
  307. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  308.  
  309.  char *link_spec = "%{!c:%{!M*:%{!E:%{!S:ld %{o*} %l\
  310.  %{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
  311.  %{y*} %{!nostdlib:%S} \
  312.  %{L*} %o %{!nostdlib:%s %L}\n }}}}";
  313.  
  314. #else
  315.  
  316. char *link_spec = "%{!c:%{!M*:%{!E:%{!S:ld %{o*} %l\
  317.  %{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
  318.  %{y*} %{!A:%{!nostdlib:%S}} \
  319.  %{L*} %o %{!nostdlib:gnulib%s %{g:-lg} %L}\n }}}}";
  320.  
  321. #endif /* not atari */
  322.  
  323. /* Accumulate a command (program name and args), and run it.  */
  324.  
  325. /* Vector of pointers to arguments in the current line of specifications.  */
  326.  
  327. char **argbuf;
  328.  
  329. /* Number of elements allocated in argbuf.  */
  330.  
  331. int argbuf_length;
  332.  
  333. /* Number of elements in argbuf currently in use (containing args).  */
  334.  
  335. int argbuf_index;
  336.  
  337. /* Number of commands executed so far.  */
  338.  
  339. int execution_count;
  340.  
  341. /* Flag indicating whether we should print the command and arguments */
  342.  
  343. unsigned char vflag;
  344.  
  345. /* Name with which this program was invoked.  */
  346.  
  347. char *programname;
  348.  
  349. /* User-specified -B prefix to attach to command names,
  350.    or 0 if none specified.  */
  351.  
  352. char *user_exec_prefix = 0;
  353.  
  354. /* Environment-specified prefix to attach to command names,
  355.    or 0 if none specified.  */
  356.  
  357. char *env_exec_prefix = 0;
  358.  
  359. /* Suffix to attach to directories searched for commands.  */
  360.  
  361. #ifndef atarist
  362.   char *machine_suffix = 0;
  363. #else
  364.   char *machine_suffix = ".ttp";
  365. #endif
  366.  
  367. /* Default prefixes to attach to command names.  */
  368.  
  369. #ifndef STANDARD_EXEC_PREFIX
  370. #  ifdef CROSSATARI
  371. #    ifdef MINIX
  372. #      define STANDARD_EXEC_PREFIX "/dsrg/bammi/cross-minix/lib/gcc-"
  373. #    else
  374. #      define STANDARD_EXEC_PREFIX "/dsrg/bammi/cross-gcc/lib/gcc-"
  375. #    endif /* MINIX */
  376. #  else /* not cross compiler */
  377. #    define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-"
  378. #  endif /* CROSSATARI */
  379. #endif /* !defined STANDARD_EXEC_PREFIX */
  380.  
  381. char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
  382. char *standard_exec_prefix_1 = "/usr/lib/gcc-";
  383.  
  384. #ifdef CROSSATARI
  385. #  ifdef MINIX
  386.     char *standard_startfile_prefix = "/dsrg/bammi/cross-minix/lib/";
  387. #  else
  388.     char *standard_startfile_prefix = "/dsrg/bammi/cross-gcc/lib/";
  389. #  endif
  390. #else
  391. #  ifdef atariminix
  392.     char *standard_startfile_prefix = "/usr/local/lib/";
  393. #  else
  394. #    ifndef STANDARD_STARTFILE_PREFIX
  395. #      define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
  396. #    endif /* !defined STANDARD_STARTFILE_PREFIX */
  397.      char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
  398. #  endif
  399. #endif
  400.  
  401. char *standard_startfile_prefix_1 = "/lib/";
  402. char *standard_startfile_prefix_2 = "/usr/lib/";
  403.  
  404. /* Clear out the vector of arguments (after a command is executed).  */
  405.  
  406. void
  407. clear_args ()
  408. {
  409.   argbuf_index = 0;
  410. }
  411.  
  412. /* Add one argument to the vector at the end.
  413.    This is done when a space is seen or at the end of the line.
  414.    If DELETE_ALWAYS is nonzero, the arg is a filename
  415.     and the file should be deleted eventually.
  416.    If DELETE_FAILURE is nonzero, the arg is a filename
  417.     and the file should be deleted if this compilation fails.  */
  418.  
  419. void
  420. store_arg (arg, delete_always, delete_failure)
  421.      char *arg;
  422.      int delete_always, delete_failure;
  423. {
  424.   if (argbuf_index + 1 == argbuf_length)
  425.     {
  426.       argbuf = (char **) realloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
  427.     }
  428.  
  429.   argbuf[argbuf_index++] = arg;
  430.   argbuf[argbuf_index] = 0;
  431.  
  432.   if (delete_always || delete_failure)
  433.     record_temp_file (arg, delete_always, delete_failure);
  434. }
  435.  
  436. /* Record the names of temporary files we tell compilers to write,
  437.    and delete them at the end of the run.  */
  438.  
  439. /* This is the common prefix we use to make temp file names.
  440.    It is chosen once for each run of this program.
  441.    It is substituted into a spec by %g.
  442.    Thus, all temp file names contain this prefix.
  443.    In practice, all temp file names start with this prefix.
  444.  
  445.    This prefix comes from the envvar TMPDIR if it is defined;
  446.    otherwise, from the P_tmpdir macro if that is defined;
  447.    otherwise, in /usr/tmp or /tmp.  */
  448.  
  449. char *temp_filename;
  450.  
  451. /* Length of the prefix.  */
  452.  
  453. int temp_filename_length;
  454.  
  455. /* Define the list of temporary files to delete.  */
  456.  
  457. struct temp_file
  458. {
  459.   char *name;
  460.   struct temp_file *next;
  461. };
  462.  
  463. /* Queue of files to delete on success or failure of compilation.  */
  464. struct temp_file *always_delete_queue;
  465. /* Queue of files to delete on failure of compilation.  */
  466. struct temp_file *failure_delete_queue;
  467.  
  468. /* Record FILENAME as a file to be deleted automatically.
  469.    ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
  470.    otherwise delete it in any case.
  471.    FAIL_DELETE nonzero means delete it if a compilation step fails;
  472.    otherwise delete it in any case.  */
  473.  
  474. void
  475. record_temp_file (filename, always_delete, fail_delete)
  476.      char *filename;
  477.      int always_delete;
  478.      int fail_delete;
  479. {
  480.   register char *name;
  481.   name = (char *) xmalloc (strlen (filename) + 1);
  482.   strcpy (name, filename);
  483.  
  484.   if (always_delete)
  485.     {
  486.       register struct temp_file *temp;
  487.       for (temp = always_delete_queue; temp; temp = temp->next)
  488.     if (! strcmp (name, temp->name))
  489.       goto already1;
  490.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  491.       temp->next = always_delete_queue;
  492.       temp->name = name;
  493.       always_delete_queue = temp;
  494.     already1:;
  495.     }
  496.  
  497.   if (fail_delete)
  498.     {
  499.       register struct temp_file *temp;
  500.       for (temp = failure_delete_queue; temp; temp = temp->next)
  501.     if (! strcmp (name, temp->name))
  502.       goto already2;
  503.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  504.       temp->next = failure_delete_queue;
  505.       temp->name = name;
  506.       failure_delete_queue = temp;
  507.     already2:;
  508.     }
  509. }
  510.  
  511. /* Delete all the temporary files whose names we previously recorded.  */
  512.  
  513. void
  514. delete_temp_files ()
  515. {
  516.   register struct temp_file *temp;
  517.  
  518.   for (temp = always_delete_queue; temp; temp = temp->next)
  519.     {
  520. #ifdef DEBUG
  521.       int i;
  522.       printf ("Delete %s? (y or n) ", temp->name);
  523.       fflush (stdout);
  524.       i = getchar ();
  525.       if (i != '\n')
  526.     while (getchar () != '\n') ;
  527.       if (i == 'y' || i == 'Y')
  528. #endif /* DEBUG */
  529.     {
  530.       if (unlink (temp->name) < 0)
  531.         if (vflag)
  532.           perror_with_name (temp->name);
  533.     }
  534.     }
  535.  
  536.   always_delete_queue = 0;
  537. }
  538.  
  539. /* Delete all the files to be deleted on error.  */
  540.  
  541. void
  542. delete_failure_queue ()
  543. {
  544.   register struct temp_file *temp;
  545.  
  546.   for (temp = failure_delete_queue; temp; temp = temp->next)
  547.     {
  548. #ifdef DEBUG
  549.       int i;
  550.       printf ("Delete %s? (y or n) ", temp->name);
  551.       fflush (stdout);
  552.       i = getchar ();
  553.       if (i != '\n')
  554.     while (getchar () != '\n') ;
  555.       if (i == 'y' || i == 'Y')
  556. #endif /* DEBUG */
  557.     {
  558.       if (unlink (temp->name) < 0)
  559.         if (vflag)
  560.           perror_with_name (temp->name);
  561.     }
  562.     }
  563. }
  564.  
  565. void
  566. clear_failure_queue ()
  567. {
  568.   failure_delete_queue = 0;
  569. }
  570.  
  571. /* Compute a string to use as the base of all temporary file names.
  572.    It is substituted for %g.  */
  573.  
  574. void
  575. choose_temp_base ()
  576. {
  577.   extern char *getenv ();
  578.   int len;
  579. #if (!(defined(atarist) || defined(atariminix)))
  580.   char *base = getenv ("TMPDIR");
  581. #else
  582.   char *base = getenv ("TEMP");
  583.   if(base == (char *)0)
  584.       base = getenv("TMPDIR");
  585. #endif
  586.  
  587.   if (base == (char *)0)
  588.     {
  589. #ifdef P_tmpdir
  590.       if (access (P_tmpdir, R_OK | W_OK) == 0)
  591.     base = P_tmpdir;
  592. #endif
  593.       if (base == (char *)0)
  594.     {
  595.       if (access ("/usr/tmp", R_OK | W_OK) == 0)
  596.         base = "/usr/tmp/";
  597.       else
  598.         base = "/tmp/";
  599.     }
  600.     }
  601.  
  602.   len = strlen (base);
  603.   temp_filename = (char *) xmalloc (len + sizeof("/ccXXXXXX"));
  604.   strcpy (temp_filename, base);
  605.   if (len > 0 && temp_filename[len-1] != '/')
  606.     temp_filename[len++] = '/';
  607.   strcpy (temp_filename + len, "ccXXXXXX");
  608.  
  609.   mktemp (temp_filename);
  610.   temp_filename_length = strlen (temp_filename);
  611. }
  612.  
  613. /* Search for an execute file through our search path.
  614.    Return 0 if not found, otherwise return its name, allocated with malloc.  */
  615.  
  616. static char *
  617. find_exec_file (prog)
  618.      char *prog;
  619. {
  620.   int win = 0;
  621.   char *temp;
  622.   int size;
  623.  
  624.   size = strlen (standard_exec_prefix);
  625.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  626.     size = strlen (user_exec_prefix);
  627.   if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
  628.     size = strlen (env_exec_prefix);
  629.   if (strlen (standard_exec_prefix_1) > size)
  630.     size = strlen (standard_exec_prefix_1);
  631.   size += strlen (prog) + 1;
  632.   if (machine_suffix)
  633.     size += strlen (machine_suffix) + 1;
  634.   temp = (char *) xmalloc (size);
  635.  
  636.   /* Determine the filename to execute.  */
  637.  
  638.   if (user_exec_prefix)
  639.     {
  640.       if (machine_suffix)
  641.     {
  642.       strcpy (temp, user_exec_prefix);
  643.       strcat (temp, prog);
  644.       strcat (temp, machine_suffix);
  645.       win = (access (temp, X_OK) == 0);
  646.     }
  647.       if (!win)
  648.     {
  649.       strcpy (temp, user_exec_prefix);
  650.       strcat (temp, prog);
  651.       win = (access (temp, X_OK) == 0);
  652.     }
  653.     }
  654.  
  655.   if (!win && env_exec_prefix)
  656.     {
  657.       if (machine_suffix)
  658.     {
  659.       strcpy (temp, env_exec_prefix);
  660.       strcat (temp, prog);
  661.       strcat (temp, machine_suffix);
  662.       win = (access (temp, X_OK) == 0);
  663.     }
  664.       if (!win)
  665.     {
  666.       strcpy (temp, env_exec_prefix);
  667.       strcat (temp, prog);
  668.       win = (access (temp, X_OK) == 0);
  669.     }
  670.     }
  671.  
  672.   if (!win)
  673.     {
  674.       if (machine_suffix)
  675.     {
  676.       strcpy (temp, standard_exec_prefix);
  677.       strcat (temp, prog);
  678.       strcat (temp, machine_suffix);
  679.       win = (access (temp, X_OK) == 0);
  680.     }
  681.       if (!win)
  682.     {
  683.       strcpy (temp, standard_exec_prefix);
  684.       strcat (temp, prog);
  685.       win = (access (temp, X_OK) == 0);
  686.     }
  687.     }
  688.  
  689.   if (!win)
  690.     {
  691.       if (machine_suffix)
  692.     {
  693.       strcpy (temp, standard_exec_prefix_1);
  694.       strcat (temp, prog);
  695.       strcat (temp, machine_suffix);
  696.       win = (access (temp, X_OK) == 0);
  697.     }
  698.       if (!win)
  699.     {
  700.       strcpy (temp, standard_exec_prefix_1);
  701.       strcat (temp, prog);
  702.       win = (access (temp, X_OK) == 0);
  703.     }
  704.     }
  705.  
  706.   if (win)
  707.     return temp;
  708.   else
  709.     return 0;
  710. }
  711.  
  712. /* stdin file number.  */
  713. #define STDIN_FILE_NO 0
  714.  
  715. /* stdout file number.  */
  716. #define STDOUT_FILE_NO 1
  717.  
  718. /* value of `pipe': port index for reading.  */
  719. #define READ_PORT 0
  720.  
  721. /* value of `pipe': port index for writing.  */
  722. #define WRITE_PORT 1
  723.  
  724. /* Pipe waiting from last process, to be used as input for the next one.
  725.    Value is STDIN_FILE_NO if no pipe is waiting
  726.    (i.e. the next command is the first of a group).  */
  727.  
  728. int last_pipe_input;
  729.  
  730. /* Fork one piped subcommand.  FUNC is the system call to use
  731.    (either execv or execvp).  ARGV is the arg vector to use.
  732.    NOT_LAST is nonzero if this is not the last subcommand
  733.    (i.e. its output should be piped to the next one.)  */
  734.  
  735. #ifndef atarist
  736. static int
  737. pexecute (func, program, argv, not_last)
  738.      char *program;
  739.      int (*func)();
  740.      char *argv[];
  741.      int not_last;
  742. {
  743.   int pid;
  744.   int pdes[2];
  745.   int input_desc = last_pipe_input;
  746.   int output_desc = STDOUT_FILE_NO;
  747.  
  748.   /* If this isn't the last process, make a pipe for its output,
  749.      and record it as waiting to be the input to the next process.  */
  750.  
  751.   if (not_last)
  752.     {
  753.       if (pipe (pdes) < 0)
  754.     pfatal_with_name ("pipe");
  755.       output_desc = pdes[WRITE_PORT];
  756.       last_pipe_input = pdes[READ_PORT];
  757.     }
  758.   else
  759.     last_pipe_input = STDIN_FILE_NO;
  760.  
  761.   pid = vfork ();
  762.  
  763.   switch (pid)
  764.     {
  765.     case -1:
  766.       pfatal_with_name ("vfork");
  767.       break;
  768.  
  769.     case 0: /* child */
  770.       /* Move the input and output pipes into place, if nec.  */
  771.       if (input_desc != STDIN_FILE_NO)
  772.     {
  773.       close (STDIN_FILE_NO);
  774.       dup (input_desc);
  775.       close (input_desc);
  776.     }
  777.       if (output_desc != STDOUT_FILE_NO)
  778.     {
  779.       close (STDOUT_FILE_NO);
  780.       dup (output_desc);
  781.       close (output_desc);
  782.     }
  783.  
  784.       /* Close the parent's descs that aren't wanted here.  */
  785.       if (last_pipe_input != STDIN_FILE_NO)
  786.     close (last_pipe_input);
  787.  
  788.       /* Exec the program.  */
  789.       (*func) (program, argv);
  790.       perror_exec (program);
  791.       exit (-1);
  792.       /* NOTREACHED */
  793.  
  794.     default:
  795.       /* In the parent, after forking.
  796.      Close the descriptors that we made for this child.  */
  797.       if (input_desc != STDIN_FILE_NO)
  798.     close (input_desc);
  799.       if (output_desc != STDOUT_FILE_NO)
  800.     close (output_desc);
  801.  
  802.       /* Return child's process number.  */
  803.       return pid;
  804.     }
  805. }
  806. #endif /* atarist */
  807.  
  808. /* Execute the command specified by the arguments on the current line of spec.
  809.    When using pipes, this includes several piped-together commands
  810.    with `|' between them.
  811.  
  812.    Return 0 if successful, -1 if failed.  */
  813.  
  814. int
  815. execute ()
  816. {
  817.   int i, j;
  818.   int n_commands;        /* # of command.  */
  819.   char *string;
  820.   struct command
  821.     {
  822.       char *prog;        /* program name.  */
  823.       char **argv;        /* vector of args.  */
  824. #ifndef atarist
  825.       int pid;            /* pid of process for this command.  */
  826. #endif
  827.     };
  828.  
  829.   struct command *commands;    /* each command buffer with above info.  */
  830.  
  831. #ifndef atarist
  832.   /* Count # of piped commands.  */
  833.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  834.     if (strcmp (argbuf[i], "|") == 0)
  835.      n_commands++;
  836. #else
  837.   n_commands = 1;
  838. #endif
  839.   
  840.   /* Get storage for each command.  */
  841.   commands
  842.     = (struct command *) alloca (n_commands * sizeof (struct command));
  843.  
  844.   /* Split argbuf into its separate piped processes,
  845.      and record info about each one.
  846.      Also search for the programs that are to be run.  */
  847.  
  848.   commands[0].prog = argbuf[0]; /* first command.  */
  849.   commands[0].argv = &argbuf[0];
  850.   string = find_exec_file (commands[0].prog);
  851.   if (string)
  852.     commands[0].argv[0] = string;
  853.  
  854. #ifndef atarist
  855.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  856.     if (strcmp (argbuf[i], "|") == 0)
  857.       {                /* each command.  */
  858.     argbuf[i] = 0;    /* termination of command args.  */
  859.     commands[n_commands].prog = argbuf[i + 1];
  860.     commands[n_commands].argv = &argbuf[i + 1];
  861.     string = find_exec_file (commands[n_commands].prog);
  862.     if (string)
  863.       commands[n_commands].argv[0] = string;
  864.     n_commands++;
  865.       }
  866. #else
  867.   n_commands = 1;
  868. #endif
  869.   
  870.   argbuf[argbuf_index] = 0;
  871.  
  872.   /* If -v, print what we are about to do, and maybe query.  */
  873.  
  874.   if (vflag)
  875.     {
  876.       /* Print each piped command as a separate line.  */
  877.       for (i = 0; i < n_commands ; i++)
  878.     {
  879.       char **j;
  880.  
  881.       for (j = commands[i].argv; *j; j++)
  882.         fprintf (stderr, " %s", *j);
  883.  
  884. #ifndef atarist
  885.       /* Print a pipe symbol after all but the last command.  */
  886.       if (i + 1 != n_commands)
  887.         fprintf (stderr, " |");
  888. #endif
  889.       fprintf (stderr, "\n");
  890.       }
  891.       fflush (stderr);
  892. #ifdef DEBUG
  893.       fprintf (stderr, "\nGo ahead? (y or n) ");
  894.       fflush (stderr);
  895.       j = getchar ();
  896.       if (j != '\n')
  897.     while (getchar () != '\n') ;
  898.       if (j != 'y' && j != 'Y')
  899.     return 0;
  900. #endif /* DEBUG */
  901.     }
  902.  
  903. #ifndef atarist
  904.   /* Run each piped subprocess.  */
  905.  
  906.   last_pipe_input = STDIN_FILE_NO;
  907.   for (i = 0; i < n_commands; i++)
  908.     {
  909. #ifndef atariminix
  910.       extern int execv(), execvp();
  911.       char *string = commands[i].argv[0];
  912.  
  913.       commands[i].pid = pexecute ((string != commands[i].prog ? execv : execvp),
  914.                   string, commands[i].argv,
  915.                   i + 1 < n_commands);
  916. #else
  917.       extern int execv();
  918.       char *string = commands[i].argv[0];
  919.  
  920.       commands[i].pid = pexecute (execv,
  921.                   string, commands[i].argv,
  922.                   i + 1 < n_commands);
  923. #endif
  924.       if (string != commands[i].prog)
  925.     free (string);
  926.     }
  927.  
  928.   execution_count++;
  929.  
  930.   /* Wait for all the subprocesses to finish.
  931.      We don't care what order they finish in;
  932.      we know that N_COMMANDS waits will get them all.  */
  933.  
  934.   {
  935.     int ret_code = 0;
  936.  
  937.     for (i = 0; i < n_commands; i++)
  938.       {
  939.     int status;
  940.     int pid;
  941.     char *prog;
  942.  
  943.     pid = wait (&status);
  944.     if (pid < 0)
  945.       abort ();
  946.  
  947.     if (status != 0)
  948.       {
  949.         int j;
  950.         for (j = 0; j < n_commands; j++)
  951.           if (commands[j].pid == pid)
  952.         prog = commands[j].prog;
  953.  
  954.         if ((status & 0x7F) != 0)
  955.           fatal ("Program %s got fatal signal %d.", prog, (status & 0x7F));
  956.         if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
  957.           ret_code = -1;
  958.       }
  959.       }
  960.     return ret_code;
  961.   }
  962. #else  /* atarist */
  963.  {
  964.      register int iii;
  965.      char **j;
  966.  
  967.      execution_count++;
  968.      for (i = 0; i < n_commands ; i++)
  969.      {
  970.      j = commands[i].argv;
  971.          iii = spawnve(0, j[0], j, NULL);
  972.      if(iii != 0)
  973.          return -1;
  974.      }
  975.      return 0;
  976.  }
  977. #endif /* atarist */  
  978. }
  979.  
  980.  
  981. /* Find all the switches given to us
  982.    and make a vector describing them.
  983.    The elements of the vector a strings, one per switch given.
  984.    If a switch uses the following argument, then the `part1' field
  985.    is the switch itself and the `part2' field is the following argument.
  986.    The `valid' field is nonzero if any spec has looked at this switch;
  987.    if it remains zero at the end of the run, it must be meaningless.  */
  988.  
  989. struct switchstr
  990. {
  991.   char *part1;
  992.   char *part2;
  993.   int valid;
  994. };
  995.  
  996. struct switchstr *switches;
  997.  
  998. int n_switches;
  999.  
  1000. /* Also a vector of input files specified.  */
  1001.  
  1002. char **infiles;
  1003.  
  1004. int n_infiles;
  1005.  
  1006. /* And a vector of corresponding output files is made up later.  */
  1007.  
  1008. char **outfiles;
  1009.  
  1010. /* Create the vector `switches' and its contents.
  1011.    Store its length in `n_switches'.  */
  1012.  
  1013. void
  1014. process_command (argc, argv)
  1015.      int argc;
  1016.      char **argv;
  1017. {
  1018.   extern char *getenv ();
  1019.   register int i;
  1020.   n_switches = 0;
  1021.   n_infiles = 0;
  1022.  
  1023.   env_exec_prefix = getenv ("GCC_EXEC_PREFIX");
  1024.  
  1025.   /* Scan argv twice.  Here, the first time, just count how many switches
  1026.      there will be in their vector, and how many input files in theirs.
  1027.      Here we also parse the switches that cc itself uses (e.g. -v).  */
  1028.  
  1029.   for (i = 1; i < argc; i++)
  1030.     {
  1031.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  1032.     {
  1033.       register char *p = &argv[i][1];
  1034.       register int c = *p;
  1035.  
  1036.       switch (c)
  1037.         {
  1038.         case 'b':
  1039.           machine_suffix = p + 1;
  1040.           break;
  1041.  
  1042.         case 'B':
  1043.           user_exec_prefix = p + 1;
  1044.           break;
  1045.  
  1046.         case 'v':    /* Print our subcommands and print versions.  */
  1047.           vflag++;
  1048.           n_switches++;
  1049.           break;
  1050.  
  1051.         default:
  1052.           n_switches++;
  1053.  
  1054.           if (SWITCH_TAKES_ARG (c) && p[1] == 0)
  1055.         i++;
  1056.           else if (WORD_SWITCH_TAKES_ARG (p))
  1057.         i++;
  1058.         }
  1059.     }
  1060.       else
  1061.     n_infiles++;
  1062.     }
  1063.  
  1064.   /* Then create the space for the vectors and scan again.  */
  1065.  
  1066.   switches = ((struct switchstr *)
  1067.           xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
  1068.   infiles = (char **) xmalloc ((n_infiles + 1) * sizeof (char *));
  1069.   n_switches = 0;
  1070.   n_infiles = 0;
  1071.  
  1072.   /* This, time, copy the text of each switch and store a pointer
  1073.      to the copy in the vector of switches.
  1074.      Store all the infiles in their vector.  */
  1075.  
  1076.   for (i = 1; i < argc; i++)
  1077.     {
  1078.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  1079.     {
  1080.       register char *p = &argv[i][1];
  1081.       register int c = *p;
  1082.  
  1083.       if (c == 'B' || c == 'b')
  1084.         continue;
  1085.       switches[n_switches].part1 = p;
  1086.       if ((SWITCH_TAKES_ARG (c) && p[1] == 0)
  1087.           || WORD_SWITCH_TAKES_ARG (p))
  1088.         switches[n_switches].part2 = argv[++i];
  1089.       else
  1090.         switches[n_switches].part2 = 0;
  1091.       switches[n_switches].valid = 0;
  1092.       n_switches++;
  1093.     }
  1094.       else
  1095.     infiles[n_infiles++] = argv[i];
  1096.     }
  1097.  
  1098.   switches[n_switches].part1 = 0;
  1099.   infiles[n_infiles] = 0;
  1100. }
  1101.  
  1102. /* Process a spec string, accumulating and running commands.  */
  1103.  
  1104. /* These variables describe the input file name.
  1105.    input_file_number is the index on outfiles of this file,
  1106.    so that the output file name can be stored for later use by %o.
  1107.    input_basename is the start of the part of the input file
  1108.    sans all directory names, and basename_length is the number
  1109.    of characters starting there excluding the suffix .c or whatever.  */
  1110.  
  1111. char *input_filename;
  1112. int input_file_number;
  1113. int input_filename_length;
  1114. int basename_length;
  1115. char *input_basename;
  1116.  
  1117. /* These are variables used within do_spec and do_spec_1.  */
  1118.  
  1119. /* Nonzero if an arg has been started and not yet terminated
  1120.    (with space, tab or newline).  */
  1121. int arg_going;
  1122.  
  1123. /* Nonzero means %d or %g has been seen; the next arg to be terminated
  1124.    is a temporary file name.  */
  1125. int delete_this_arg;
  1126.  
  1127. /* Nonzero means %w has been seen; the next arg to be terminated
  1128.    is the output file name of this compilation.  */
  1129. int this_is_output_file;
  1130.  
  1131. /* Nonzero means %s has been seen; the next arg to be terminated
  1132.    is the name of a library file and we should try the standard
  1133.    search dirs for it.  */
  1134. int this_is_library_file;
  1135.  
  1136. #ifdef atarist
  1137. /* I don't know why this is necessary.  Recursive calls to do_spec_1
  1138.    end up ignoring the error code from calls to execute().  That causes
  1139.    do_spec to get a 0 return value, and do_spec_1("\n"), which causes the
  1140.    command to get executed again.
  1141. */
  1142. int execute_return_error = 0;
  1143. #endif
  1144.  
  1145.  
  1146. /* Process the spec SPEC and run the commands specified therein.
  1147.    Returns 0 if the spec is successfully processed; -1 if failed.  */
  1148.  
  1149. int
  1150. do_spec (spec)
  1151.      char *spec;
  1152. {
  1153.   int value;
  1154.  
  1155.   clear_args ();
  1156.   arg_going = 0;
  1157.   delete_this_arg = 0;
  1158.   this_is_output_file = 0;
  1159.   this_is_library_file = 0;
  1160.  
  1161.   value = do_spec_1 (spec, 0);
  1162.  
  1163.   /* Force out any unfinished command.
  1164.      If -pipe, this forces out the last command if it ended in `|'.  */
  1165. #ifdef atarist
  1166.   if (!value && execute_return_error)
  1167.     {
  1168.     value = execute_return_error;
  1169.     execute_return_error = 0;
  1170.     }
  1171. #endif
  1172.   if (value == 0)
  1173.     {
  1174. #ifndef atarist
  1175.       if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  1176.     argbuf_index--;
  1177. #endif
  1178.  
  1179.       if (argbuf_index > 0)
  1180.     value = execute ();
  1181.     }
  1182.  
  1183.   return value;
  1184. }
  1185.  
  1186. /* Process the sub-spec SPEC as a portion of a larger spec.
  1187.    This is like processing a whole spec except that we do
  1188.    not initialize at the beginning and we do not supply a
  1189.    newline by default at the end.
  1190.    INSWITCH nonzero means don't process %-sequences in SPEC;
  1191.    in this case, % is treated as an ordinary character.
  1192.    This is used while substituting switches.
  1193.    INSWITCH nonzero also causes SPC not to terminate an argument.
  1194.  
  1195.    Value is zero unless a line was finished
  1196.    and the command on that line reported an error.  */
  1197.  
  1198. int
  1199. do_spec_1 (spec, inswitch)
  1200.      char *spec;
  1201.      int inswitch;
  1202. {
  1203.   register char *p = spec;
  1204.   register int c;
  1205.   char *string;
  1206.   extern char *find_file();    /* ++jrb */
  1207.  
  1208.   while (c = *p++)
  1209.     /* If substituting a switch, treat all chars like letters.
  1210.        Otherwise, NL, SPC, TAB and % are special.  */
  1211.     switch (inswitch ? 'a' : c)
  1212.       {
  1213. #ifdef atarist
  1214. /* this stuff added by jrd.  if see '$', expect name of env var, delimited
  1215.    by '$'.  Find it's value, and subst it in.
  1216.  
  1217.    modified by ERS to only collect things that look like names; this
  1218.    saves e.g. the -$ arg. to cpp from getting munged */
  1219.  
  1220.       case '$':
  1221.       {
  1222.     char varname[32];        /* should be enough */
  1223.     char * value;            /* deciphered value string */
  1224.     int i;
  1225.  
  1226.     for (i = 0 ; ((c = *p) != '$') && isalnum(c) && i < 31 ; i++, p++)
  1227.         varname[i] = c;
  1228.     varname[i] = '\0';
  1229.     if (strlen(varname) > 0) /* ++jrb fix */
  1230.     {
  1231.                  ++p;    /* skip trailing '$' */
  1232.         value = (char * )getenv(varname); /* ++jrb fix */
  1233.         if (value)
  1234.             do_spec_1(value, 0);
  1235.             else
  1236.             do_spec_1(".", 0);    /* a compleat kludge... */
  1237.     }
  1238.         else
  1239.         {
  1240.              obstack_1grow(&obstack, '$');
  1241.              arg_going = 1;
  1242.         }
  1243.       }
  1244.      break;
  1245. #endif /* atarist */
  1246.       case '\n':
  1247.     /* End of line: finish any pending argument,
  1248.        then run the pending command if one has been started.  */
  1249.     if (arg_going)
  1250.       {
  1251.         obstack_1grow (&obstack, 0);
  1252.         string = obstack_finish (&obstack);
  1253.         if (this_is_library_file)
  1254.           string = find_file (string);
  1255.         store_arg (string, delete_this_arg, this_is_output_file);
  1256.         if (this_is_output_file)
  1257.           outfiles[input_file_number] = string;
  1258.       }
  1259.     arg_going = 0;
  1260.  
  1261.     if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  1262.       {
  1263.         int i;
  1264.         for (i = 0; i < n_switches; i++)
  1265.           if (!strcmp (switches[i].part1, "pipe"))
  1266.         break;
  1267.  
  1268.         /* A `|' before the newline means use a pipe here,
  1269.            but only if -pipe was specified.
  1270.            Otherwise, execute now and don't pass the `|' as an arg.  */
  1271.         if (i < n_switches)
  1272.           {
  1273.         switches[i].valid = 1;
  1274.         break;
  1275.           }
  1276.         else
  1277.           argbuf_index--;
  1278.       }
  1279.  
  1280.     if (argbuf_index > 0)
  1281.       {
  1282.         int value = execute ();
  1283. #ifdef atarist
  1284.         if (!execute_return_error)
  1285.             execute_return_error = value;
  1286. #endif
  1287.         if (value)
  1288.           return value;
  1289.       }
  1290.     /* Reinitialize for a new command, and for a new argument.  */
  1291.     clear_args ();
  1292.     arg_going = 0;
  1293.     delete_this_arg = 0;
  1294.     this_is_output_file = 0;
  1295.     this_is_library_file = 0;
  1296.     break;
  1297.  
  1298.       case '|':
  1299.     /* End any pending argument.  */
  1300.     if (arg_going)
  1301.       {
  1302.         obstack_1grow (&obstack, 0);
  1303.         string = obstack_finish (&obstack);
  1304.         if (this_is_library_file)
  1305.           string = find_file (string);
  1306.         store_arg (string, delete_this_arg, this_is_output_file);
  1307.         if (this_is_output_file)
  1308.           outfiles[input_file_number] = string;
  1309.       }
  1310.  
  1311.     /* Use pipe */
  1312.     obstack_1grow (&obstack, c);
  1313.     arg_going = 1;
  1314.     break;
  1315.  
  1316.       case '\t':
  1317.       case ' ':
  1318.     /* Space or tab ends an argument if one is pending.  */
  1319.     if (arg_going)
  1320.       {
  1321.         obstack_1grow (&obstack, 0);
  1322.         string = obstack_finish (&obstack);
  1323.         if (this_is_library_file)
  1324.           string = find_file (string);
  1325.         store_arg (string, delete_this_arg, this_is_output_file);
  1326.         if (this_is_output_file)
  1327.           outfiles[input_file_number] = string;
  1328.       }
  1329.     /* Reinitialize for a new argument.  */
  1330.     arg_going = 0;
  1331.     delete_this_arg = 0;
  1332.     this_is_output_file = 0;
  1333.     this_is_library_file = 0;
  1334.     break;
  1335.  
  1336.       case '%':
  1337.     switch (c = *p++)
  1338.       {
  1339.       case 0:
  1340.         fatal ("Invalid specification!  Bug in cc.");
  1341.  
  1342.       case 'b':
  1343.         obstack_grow (&obstack, input_basename, basename_length);
  1344.         arg_going = 1;
  1345.         break;
  1346.  
  1347.       case 'd':
  1348.         delete_this_arg = 2;
  1349.         break;
  1350.  
  1351.       case 'e':
  1352.         /* {...:%efoo} means report an error with `foo' as error message
  1353.            and don't execute any more commands for this file.  */
  1354.         {
  1355.           char *q = p;
  1356.           char *buf;
  1357.           while (*p != 0 && *p != '\n') p++;
  1358.           buf = (char *) alloca (p - q + 1);
  1359.           strncpy (buf, q, p - q);
  1360.           error ("%s", buf);
  1361.           return -1;
  1362.         }
  1363.         break;
  1364.  
  1365.       case 'g':
  1366.         obstack_grow (&obstack, temp_filename, temp_filename_length);
  1367.         delete_this_arg = 1;
  1368.         arg_going = 1;
  1369.         break;
  1370.  
  1371.       case 'i':
  1372.         obstack_grow (&obstack, input_filename, input_filename_length);
  1373.         arg_going = 1;
  1374.         break;
  1375.  
  1376.       case 'o':
  1377.         {
  1378.           register int f;
  1379.           for (f = 0; f < n_infiles; f++)
  1380.         store_arg (outfiles[f], 0, 0);
  1381.         }
  1382.         break;
  1383.  
  1384.       case 's':
  1385.         this_is_library_file = 1;
  1386.         break;
  1387.  
  1388.       case 'w':
  1389.         this_is_output_file = 1;
  1390.         break;
  1391.  
  1392.       case '{':
  1393.         p = handle_braces (p);
  1394.         if (p == 0)
  1395.           return -1;
  1396.         break;
  1397.  
  1398.       case '%':
  1399.         obstack_1grow (&obstack, '%');
  1400.         break;
  1401.  
  1402. /*** The rest just process a certain constant string as a spec.  */
  1403.  
  1404.       case '1':
  1405.         do_spec_1 (CC1_SPEC, 0);
  1406.         break;
  1407.  
  1408.       case 'a':
  1409.         do_spec_1 (ASM_SPEC, 0);
  1410.         break;
  1411.  
  1412.       case 'c':
  1413.         do_spec_1 (SIGNED_CHAR_SPEC, 0);
  1414.         break;
  1415.  
  1416.       case 'C':
  1417.         do_spec_1 (CPP_SPEC, 0);
  1418.         break;
  1419.  
  1420.       case 'l':
  1421.         do_spec_1 (LINK_SPEC, 0);
  1422.         break;
  1423.  
  1424.       case 'L':
  1425.         do_spec_1 (LIB_SPEC, 0);
  1426.         break;
  1427.  
  1428.       case 'p':
  1429.         do_spec_1 (CPP_PREDEFINES, 0);
  1430.         break;
  1431.  
  1432.       case 'P':
  1433.         {
  1434.           char *x = (char *) alloca (strlen (CPP_PREDEFINES) * 2 + 1);
  1435.           char *buf = x;
  1436.           char *y = CPP_PREDEFINES;
  1437.  
  1438.           /* Copy all of CPP_PREDEFINES into BUF,
  1439.          but put __ after every -D and at the end of each arg,  */
  1440.           while (1)
  1441.         {
  1442.           if (! strncmp (y, "-D", 2))
  1443.             {
  1444.               *x++ = '-';
  1445.               *x++ = 'D';
  1446.               *x++ = '_';
  1447.               *x++ = '_';
  1448.               y += 2;
  1449.             }
  1450.           else if (*y == ' ' || *y == 0)
  1451.             {
  1452.               *x++ = '_';
  1453.               *x++ = '_';
  1454.               if (*y == 0)
  1455.             break;
  1456.               else
  1457.             *x++ = *y++;
  1458.             }
  1459.           else
  1460.             *x++ = *y++;
  1461.         }
  1462.           *x = 0;
  1463.  
  1464.           do_spec_1 (buf, 0);
  1465.         }
  1466.         break;
  1467.  
  1468.       case 'S':
  1469.         do_spec_1 (STARTFILE_SPEC, 0);
  1470.         break;
  1471.  
  1472.       default:
  1473. #ifdef atarist
  1474.         fprintf(stderr, "Bogus char '%c' found at pos %d of spec '%s'\n",
  1475.         c, (p - spec - 1), spec);
  1476. #endif
  1477.         abort ();
  1478.       }
  1479.     break;
  1480.  
  1481.       default:
  1482.     /* Ordinary character: put it into the current argument.  */
  1483.     obstack_1grow (&obstack, c);
  1484.     arg_going = 1;
  1485.       }
  1486.  
  1487.   return 0;        /* End of string */
  1488. }
  1489.  
  1490. /* Return 0 if we call do_spec_1 and that returns -1.  */
  1491.  
  1492. char *
  1493. handle_braces (p)
  1494.      register char *p;
  1495. {
  1496.   register char *q;
  1497.   char *filter;
  1498.   int pipe = 0;
  1499.   int negate = 0;
  1500.  
  1501.   if (*p == '|')
  1502.     /* A `|' after the open-brace means,
  1503.        if the test fails, output a single minus sign rather than nothing.
  1504.        This is used in %{|!pipe:...}.  */
  1505.     pipe = 1, ++p;
  1506.  
  1507.    if (*p == '!')
  1508.     /* A `!' after the open-brace negates the condition:
  1509.        succeed if the specified switch is not present.  */
  1510.     negate = 1, ++p;
  1511.  
  1512.     filter = p;
  1513.     while (*p != ':' && *p != '}') p++;
  1514.     if (*p != '}')
  1515.     {
  1516.       register int count = 1;
  1517.       q = p + 1;
  1518.       while (count > 0)
  1519.     {
  1520.       if (*q == '{')
  1521.         count++;
  1522.       else if (*q == '}')
  1523.         count--;
  1524.       else if (*q == 0)
  1525.         abort ();
  1526.       q++;
  1527.     }
  1528.     }
  1529.   else
  1530.     q = p + 1;
  1531.  
  1532.   if (p[-1] == '*' && p[0] == '}')
  1533.     {
  1534.       /* Substitute all matching switches as separate args.  */
  1535.       register int i;
  1536.       --p;
  1537.       for (i = 0; i < n_switches; i++)
  1538.     if (!strncmp (switches[i].part1, filter, p - filter))
  1539.       give_switch (i);
  1540.     }
  1541.   else
  1542.     {
  1543.       /* Test for presence of the specified switch.  */
  1544.       register int i;
  1545.       int present = 0;
  1546.  
  1547.       /* If name specified ends in *, as in {x*:...},
  1548.      check for presence of any switch name starting with x.  */
  1549.       if (p[-1] == '*')
  1550.     {
  1551.       for (i = 0; i < n_switches; i++)
  1552.         {
  1553.           if (!strncmp (switches[i].part1, filter, p - filter - 1))
  1554.         {
  1555.           switches[i].valid = 1;
  1556.           present = 1;
  1557.         }
  1558.         }
  1559.     }
  1560.       /* Otherwise, check for presence of exact name specified.  */
  1561.       else
  1562.     {
  1563.       for (i = 0; i < n_switches; i++)
  1564.         {
  1565.           if (!strncmp (switches[i].part1, filter, p - filter)
  1566.           && switches[i].part1[p - filter] == 0)
  1567.         {
  1568.           switches[i].valid = 1;
  1569.           present = 1;
  1570.           break;
  1571.         }
  1572.         }
  1573.     }
  1574.  
  1575.       /* If it is as desired (present for %{s...}, absent for %{-s...})
  1576.      then substitute either the switch or the specified
  1577.      conditional text.  */
  1578.       if (present != negate)
  1579.     {
  1580.       if (*p == '}')
  1581.         {
  1582.           give_switch (i);
  1583.         }
  1584.       else
  1585.         {
  1586.           if (do_spec_1 (save_string (p + 1, q - p - 2), 0) < 0)
  1587.         return 0;
  1588.         }
  1589.     }
  1590.         else if (pipe)
  1591.     {
  1592.       /* Here if a %{|...} conditional fails: output a minus sign,
  1593.          which means "standard output" or "standard input".  */
  1594.       do_spec_1 ("-", 0);
  1595.     }
  1596.     }
  1597.  
  1598.   return q;
  1599. }
  1600.  
  1601. /* Pass a switch to the current accumulating command
  1602.    in the same form that we received it.
  1603.    SWITCHNUM identifies the switch; it is an index into
  1604.    the vector of switches gcc received, which is `switches'.
  1605.    This cannot fail since it never finishes a command line.  */
  1606.  
  1607. give_switch (switchnum)
  1608.      int switchnum;
  1609. {
  1610.   do_spec_1 ("-", 0);
  1611.   do_spec_1 (switches[switchnum].part1, 1);
  1612.   do_spec_1 (" ", 0);
  1613.   if (switches[switchnum].part2 != 0)
  1614.     {
  1615.       do_spec_1 (switches[switchnum].part2, 1);
  1616.       do_spec_1 (" ", 0);
  1617.     }
  1618.   switches[switchnum].valid = 1;
  1619. }
  1620.  
  1621. /* Search for a file named NAME trying various prefixes including the
  1622.    user's -B prefix and some standard ones.
  1623.    Return the absolute pathname found.  If nothing is found, return NAME.  */
  1624.  
  1625. char *
  1626. find_file (name)
  1627.      char *name;
  1628. {
  1629.   int size;
  1630.   char *temp;
  1631.   int win = 0;
  1632.  
  1633.   /* Compute maximum size of NAME plus any prefix we will try.  */
  1634.  
  1635.   size = strlen (standard_exec_prefix);
  1636.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  1637.     size = strlen (user_exec_prefix);
  1638.   if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
  1639.     size = strlen (env_exec_prefix);
  1640.   if (strlen (standard_exec_prefix) > size)
  1641.     size = strlen (standard_exec_prefix);
  1642.   if (strlen (standard_exec_prefix_1) > size)
  1643.     size = strlen (standard_exec_prefix_1);
  1644.   if (strlen (standard_startfile_prefix) > size)
  1645.     size = strlen (standard_startfile_prefix);
  1646.   if (strlen (standard_startfile_prefix_1) > size)
  1647.     size = strlen (standard_startfile_prefix_1);
  1648.   if (strlen (standard_startfile_prefix_2) > size)
  1649.     size = strlen (standard_startfile_prefix_2);
  1650.   if (machine_suffix)
  1651.     size += strlen (machine_suffix) + 1;
  1652.   size += strlen (name) + 1;
  1653.  
  1654.   temp = (char *) alloca (size);
  1655.  
  1656.   if (user_exec_prefix)
  1657.     {
  1658.       if (machine_suffix)
  1659.     {
  1660.       strcpy (temp, user_exec_prefix);
  1661.       strcat (temp, name);
  1662.       strcat (temp, machine_suffix);
  1663.       win = (access (temp, R_OK) == 0);
  1664.     }
  1665.       if (!win)
  1666.     {
  1667.       strcpy (temp, user_exec_prefix);
  1668.       strcat (temp, name);
  1669.       win = (access (temp, R_OK) == 0);
  1670.     }
  1671.     }
  1672.  
  1673.   if (!win && env_exec_prefix)
  1674.     {
  1675.       if (machine_suffix)
  1676.     {
  1677.       strcpy (temp, env_exec_prefix);
  1678.       strcat (temp, name);
  1679.       strcat (temp, machine_suffix);
  1680.       win = (access (temp, R_OK) == 0);
  1681.     }
  1682.       if (!win)
  1683.     {
  1684.       strcpy (temp, env_exec_prefix);
  1685.       strcat (temp, name);
  1686.       win = (access (temp, R_OK) == 0);
  1687.     }
  1688.     }
  1689.  
  1690.   if (!win)
  1691.     {
  1692.       if (machine_suffix)
  1693.     {
  1694.       strcpy (temp, standard_exec_prefix);
  1695.       strcat (temp, name);
  1696.       strcat (temp, machine_suffix);
  1697.       win = (access (temp, R_OK) == 0);
  1698.     }
  1699.       if (!win)
  1700.     {
  1701.       strcpy (temp, standard_exec_prefix);
  1702.       strcat (temp, name);
  1703.       win = (access (temp, R_OK) == 0);
  1704.     }
  1705.     }
  1706.  
  1707.   if (!win)
  1708.     {
  1709.       if (machine_suffix)
  1710.     {
  1711.       strcpy (temp, standard_exec_prefix_1);
  1712.       strcat (temp, name);
  1713.       strcat (temp, machine_suffix);
  1714.       win = (access (temp, R_OK) == 0);
  1715.     }
  1716.       if (!win)
  1717.     {
  1718.       strcpy (temp, standard_exec_prefix_1);
  1719.       strcat (temp, name);
  1720.       win = (access (temp, R_OK) == 0);
  1721.     }
  1722.     }
  1723.  
  1724.   if (!win)
  1725.     {
  1726.       if (machine_suffix)
  1727.     {
  1728.       strcpy (temp, standard_startfile_prefix);
  1729.       strcat (temp, name);
  1730.       strcat (temp, machine_suffix);
  1731.       win = (access (temp, R_OK) == 0);
  1732.     }
  1733.       if (!win)
  1734.     {
  1735.       strcpy (temp, standard_startfile_prefix);
  1736.       strcat (temp, name);
  1737.       win = (access (temp, R_OK) == 0);
  1738.     }
  1739.     }
  1740.  
  1741.   if (!win)
  1742.     {
  1743.       if (machine_suffix)
  1744.     {
  1745.       strcpy (temp, standard_startfile_prefix_1);
  1746.       strcat (temp, name);
  1747.       strcat (temp, machine_suffix);
  1748.       win = (access (temp, R_OK) == 0);
  1749.     }
  1750.       if (!win)
  1751.     {
  1752.       strcpy (temp, standard_startfile_prefix_1);
  1753.       strcat (temp, name);
  1754.       win = (access (temp, R_OK) == 0);
  1755.     }
  1756.     }
  1757.  
  1758.   if (!win)
  1759.     {
  1760.       if (machine_suffix)
  1761.     {
  1762.       strcpy (temp, standard_startfile_prefix_2);
  1763.       strcat (temp, name);
  1764.       strcat (temp, machine_suffix);
  1765.       win = (access (temp, R_OK) == 0);
  1766.     }
  1767.       if (!win)
  1768.     {
  1769.       strcpy (temp, standard_startfile_prefix_2);
  1770.       strcat (temp, name);
  1771.       win = (access (temp, R_OK) == 0);
  1772.     }
  1773.     }
  1774.  
  1775.   if (!win)
  1776.     {
  1777.       if (machine_suffix)
  1778.     {
  1779.       strcpy (temp, "./");
  1780.       strcat (temp, name);
  1781.       strcat (temp, machine_suffix);
  1782.       win = (access (temp, R_OK) == 0);
  1783.     }
  1784.       if (!win)
  1785.     {
  1786.       strcpy (temp, "./");
  1787.       strcat (temp, name);
  1788.       win = (access (temp, R_OK) == 0);
  1789.     }
  1790.     }
  1791.  
  1792.   if (win)
  1793.     return save_string (temp, strlen (temp));
  1794.   return name;
  1795. }
  1796.  
  1797. /* On fatal signals, delete all the temporary files.  */
  1798.  
  1799. #ifndef atarist
  1800. void
  1801. fatal_error (signum)
  1802.      int signum;
  1803. {
  1804.   signal (signum, SIG_DFL);
  1805.   delete_failure_queue ();
  1806.   delete_temp_files ();
  1807.   /* Get the same signal again, this time not handled,
  1808.      so its normal effect occurs.  */
  1809.   kill (getpid (), signum);
  1810. }
  1811. #endif /* atarist */
  1812.  
  1813. int
  1814. main (argc, argv)
  1815.      int argc;
  1816.      char **argv;
  1817. {
  1818.   register int i;
  1819.   int value;
  1820.   int error_count = 0;
  1821.   int linker_was_run = 0;
  1822.   char *explicit_link_files;
  1823.  
  1824. #ifdef atarist
  1825. /* turn this on if you are going to set the TOS 1.4 dont clear heap flag */
  1826. /*  _malloczero(1); */     /* zero mallocs by default */
  1827.   programname = "gcc";
  1828. #else  
  1829.   programname = argv[0];
  1830. #endif
  1831.  
  1832. #ifndef atarist
  1833.   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  1834.     signal (SIGINT, fatal_error);
  1835.   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  1836.     signal (SIGHUP, fatal_error);
  1837.   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
  1838.     signal (SIGTERM, fatal_error);
  1839. #endif
  1840.  
  1841.   argbuf_length = 10;
  1842.   argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
  1843.  
  1844.   obstack_init (&obstack);
  1845.  
  1846.   choose_temp_base ();
  1847.  
  1848.   /* Make a table of what switches there are (switches, n_switches).
  1849.      Make a table of specified input files (infiles, n_infiles).  */
  1850.  
  1851.   process_command (argc, argv);
  1852.  
  1853. #ifdef atarist
  1854. /* if no default dir specified for executables, look for an env var
  1855.    called 'GCCEXEC' and use that */
  1856.  
  1857.   if (!user_exec_prefix)
  1858.       {
  1859.     user_exec_prefix = getenv("GCCEXEC");
  1860.     }
  1861. #endif /* atarist */
  1862.  
  1863.   if (vflag)
  1864.     {
  1865.       extern char *version_string;
  1866.       fprintf (stderr, "gcc version %s\n", version_string);
  1867.       if (n_infiles == 0)
  1868.     exit (0);
  1869.     }
  1870.  
  1871.   if (n_infiles == 0)
  1872.     fatal ("No input files specified.");
  1873.  
  1874.   /* Make a place to record the compiler output file names
  1875.      that correspond to the input files.  */
  1876.  
  1877.   outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
  1878.   bzero (outfiles, n_infiles * sizeof (char *));
  1879.  
  1880.   /* Record which files were specified explicitly as link input.  */
  1881.  
  1882.   explicit_link_files = (char *) xmalloc (n_infiles);
  1883.   bzero (explicit_link_files, n_infiles);
  1884.  
  1885.   for (i = 0; i < n_infiles; i++)
  1886.     {
  1887.       register struct compiler *cp;
  1888.       int this_file_error = 0;
  1889.  
  1890.       /* Tell do_spec what to substitute for %i.  */
  1891.  
  1892.       input_filename = infiles[i];
  1893.       input_filename_length = strlen (input_filename);
  1894.       input_file_number = i;
  1895.  
  1896.       /* Use the same thing in %o, unless cp->spec says otherwise.  */
  1897.  
  1898.       outfiles[i] = input_filename;
  1899.  
  1900.       /* Figure out which compiler from the file's suffix.  */
  1901.  
  1902.       for (cp = compilers; cp->spec; cp++)
  1903.     {
  1904.       if (strlen (cp->suffix) < input_filename_length
  1905.           && !strcmp (cp->suffix,
  1906.               infiles[i] + input_filename_length
  1907.               - strlen (cp->suffix)))
  1908.         {
  1909.           /* Ok, we found an applicable compiler.  Run its spec.  */
  1910.           /* First say how much of input_filename to substitute for %b  */
  1911.           register char *p;
  1912.  
  1913.           input_basename = input_filename;
  1914.           for (p = input_filename; *p; p++)
  1915. #ifdef atarist
  1916.         if (((*p == '/') || (*p == '\\')) && (*(p-1) != ':'))
  1917.             /* allow both '\' and '/' with  our new lib */
  1918. #else
  1919.         if (*p == '/')
  1920. #endif
  1921.           input_basename = p + 1;
  1922.           basename_length = (input_filename_length - strlen (cp->suffix)
  1923.                  - (input_basename - input_filename));
  1924.           value = do_spec (cp->spec);
  1925.           if (value < 0)
  1926.         this_file_error = 1;
  1927.           break;
  1928.         }
  1929.     }
  1930.  
  1931.       /* If this file's name does not contain a recognized suffix,
  1932.      record it as explicit linker input.  */
  1933.  
  1934.       if (! cp->spec)
  1935.     explicit_link_files[i] = 1;
  1936.  
  1937.       /* Clear the delete-on-failure queue, deleting the files in it
  1938.      if this compilation failed.  */
  1939.  
  1940.       if (this_file_error)
  1941.     {
  1942.       delete_failure_queue ();
  1943.       error_count++;
  1944.     }
  1945.       /* If this compilation succeeded, don't delete those files later.  */
  1946.       clear_failure_queue ();
  1947.     }
  1948.  
  1949.   /* Run ld to link all the compiler output files.  */
  1950.  
  1951.   if (error_count == 0)
  1952.     {
  1953.       int tmp = execution_count;
  1954.       value = do_spec (link_spec);
  1955.       if (value < 0)
  1956.     error_count = 1;
  1957.       linker_was_run = (tmp != execution_count);
  1958.     }
  1959.  
  1960.   /* If options said don't run linker,
  1961.      complain about input files to be given to the linker.  */
  1962.  
  1963.   if (! linker_was_run && error_count == 0)
  1964.     for (i = 0; i < n_infiles; i++)
  1965.       if (explicit_link_files[i])
  1966.     error ("%s: linker input file unused since linking not done",
  1967.            outfiles[i]);
  1968.  
  1969.   /* Set the `valid' bits for switches that match anything in any spec.  */
  1970.  
  1971.   validate_all_switches ();
  1972.  
  1973.   /* Warn about any switches that no pass was interested in.  */
  1974.   
  1975.   for (i = 0; i < n_switches; i++)
  1976.     if (! switches[i].valid)
  1977.       error ("unrecognized option `-%s'", switches[i].part1);
  1978.  
  1979.   /* Delete some or all of the temporary files we made.  */
  1980.  
  1981.   if (error_count)
  1982.     delete_failure_queue ();
  1983.   delete_temp_files ();
  1984.  
  1985.   exit (error_count);
  1986. }
  1987.  
  1988. xmalloc (size)
  1989.      int size;
  1990. {
  1991.   register int value = malloc (size);
  1992.   if (value == 0)
  1993.     fatal ("Virtual memory full.");
  1994.   return value;
  1995. }
  1996.  
  1997. xrealloc (ptr, size)
  1998.      int ptr, size;
  1999. {
  2000.   register int value = realloc (ptr, size);
  2001.   if (value == 0)
  2002.     fatal ("Virtual memory full.");
  2003.   return value;
  2004. }
  2005.  
  2006. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  2007.  
  2008. char *
  2009. concat (s1, s2, s3)
  2010.      char *s1, *s2, *s3;
  2011. {
  2012.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  2013.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  2014.  
  2015.   strcpy (result, s1);
  2016.   strcpy (result + len1, s2);
  2017.   strcpy (result + len1 + len2, s3);
  2018.   *(result + len1 + len2 + len3) = 0;
  2019.  
  2020.   return result;
  2021. }
  2022.  
  2023. char *
  2024. save_string (s, len)
  2025.      char *s;
  2026.      int len;
  2027. {
  2028.   register char *result = (char *) xmalloc (len + 1);
  2029.  
  2030.   bcopy (s, result, len);
  2031.   result[len] = 0;
  2032.   return result;
  2033. }
  2034.  
  2035. pfatal_with_name (name)
  2036.      char *name;
  2037. {
  2038.   extern int errno, sys_nerr;
  2039.   extern char *sys_errlist[];
  2040.   char *s;
  2041.  
  2042. #ifdef atarist
  2043.   if ((errno > sys_nerr) && (errno < 0))
  2044. #else
  2045.   if (errno < sys_nerr)
  2046. #endif
  2047.     s = concat ("%s: ", sys_errlist[errno], "");
  2048.   else
  2049.     s = "cannot open %s";
  2050.   fatal (s, name);
  2051. }
  2052.  
  2053. perror_with_name (name)
  2054.      char *name;
  2055. {
  2056.   extern int errno, sys_nerr;
  2057.   extern char *sys_errlist[];
  2058.   char *s;
  2059.  
  2060. #ifdef atarist
  2061.   if ((errno > sys_nerr) && (errno < 0))
  2062. #else
  2063.   if (errno < sys_nerr)
  2064. #endif
  2065.     s = concat ("%s: ", sys_errlist[errno], "");
  2066.   else
  2067.     s = "cannot open %s";
  2068.   error (s, name);
  2069. }
  2070.  
  2071. perror_exec (name)
  2072.      char *name;
  2073. {
  2074.   extern int errno, sys_nerr;
  2075.   extern char *sys_errlist[];
  2076.   char *s;
  2077.  
  2078. #ifdef atarist
  2079.   if ((errno > sys_nerr) && (errno < 0))
  2080. #else
  2081.   if (errno < sys_nerr)
  2082. #endif
  2083.     s = concat ("installation problem, cannot exec %s: ",
  2084.         sys_errlist[errno], "");
  2085.   else
  2086.     s = "installation problem, cannot exec %s";
  2087.   error (s, name);
  2088. }
  2089.  
  2090. /* More 'friendly' abort that prints the line and file.
  2091.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  2092.  
  2093. void
  2094. fancy_abort ()
  2095. {
  2096.   fatal ("Internal gcc abort.");
  2097. }
  2098.  
  2099. #ifdef HAVE_VPRINTF
  2100.  
  2101. /* Output an error message and exit */
  2102.  
  2103. int 
  2104. fatal (va_alist)
  2105.      va_dcl
  2106. {
  2107.   va_list ap;
  2108.   char *format;
  2109.   
  2110.   va_start(ap);
  2111.   format = va_arg (ap, char *);
  2112.   vfprintf (stderr, format, ap);
  2113.   va_end (ap);
  2114.   fprintf (stderr, "\n");
  2115.   delete_temp_files (0);
  2116.   exit (1);
  2117. }  
  2118.  
  2119. error (va_alist) 
  2120.      va_dcl
  2121. {
  2122.   va_list ap;
  2123.   char *format;
  2124.  
  2125.   va_start(ap);
  2126.   format = va_arg (ap, char *);
  2127.   fprintf (stderr, "%s: ", programname);
  2128.   vfprintf (stderr, format, ap);
  2129.   va_end (ap);
  2130.  
  2131.   fprintf (stderr, "\n");
  2132. }
  2133.  
  2134. #else /* not HAVE_VPRINTF */
  2135.  
  2136. fatal (msg, arg1, arg2)
  2137.      char *msg, *arg1, *arg2;
  2138. {
  2139.   error (msg, arg1, arg2);
  2140.   delete_temp_files (0);
  2141.   exit (1);
  2142. }
  2143.  
  2144. error (msg, arg1, arg2)
  2145.      char *msg, *arg1, *arg2;
  2146. {
  2147.   fprintf (stderr, "%s: ", programname);
  2148.   fprintf (stderr, msg, arg1, arg2);
  2149.   fprintf (stderr, "\n");
  2150. }
  2151.  
  2152. #endif /* not HAVE_VPRINTF */
  2153.  
  2154.  
  2155. void
  2156. validate_all_switches ()
  2157. {
  2158.   struct compiler *comp;
  2159.   register char *p;
  2160.   register char c;
  2161.  
  2162.   for (comp = compilers; comp->spec; comp++)
  2163.     {
  2164.       p = comp->spec;
  2165.       while (c = *p++)
  2166.     if (c == '%' && *p == '{')
  2167.       /* We have a switch spec.  */
  2168.       validate_switches (p + 1);
  2169.     }
  2170.  
  2171.   p = link_spec;
  2172.   while (c = *p++)
  2173.     if (c == '%' && *p == '{')
  2174.       /* We have a switch spec.  */
  2175.       validate_switches (p + 1);
  2176.  
  2177.   /* Now notice switches mentioned in the machine-specific specs.  */
  2178.  
  2179. #ifdef ASM_SPEC
  2180.   p = ASM_SPEC;
  2181.   while (c = *p++)
  2182.     if (c == '%' && *p == '{')
  2183.       /* We have a switch spec.  */
  2184.       validate_switches (p + 1);
  2185. #endif
  2186.  
  2187. #ifdef CPP_SPEC
  2188.   p = CPP_SPEC;
  2189.   while (c = *p++)
  2190.     if (c == '%' && *p == '{')
  2191.       /* We have a switch spec.  */
  2192.       validate_switches (p + 1);
  2193. #endif
  2194.  
  2195. #ifdef SIGNED_CHAR_SPEC
  2196.   p = SIGNED_CHAR_SPEC;
  2197.   while (c = *p++)
  2198.     if (c == '%' && *p == '{')
  2199.       /* We have a switch spec.  */
  2200.       validate_switches (p + 1);
  2201. #endif
  2202.  
  2203. #ifdef CC1_SPEC
  2204.   p = CC1_SPEC;
  2205.   while (c = *p++)
  2206.     if (c == '%' && *p == '{')
  2207.       /* We have a switch spec.  */
  2208.       validate_switches (p + 1);
  2209. #endif
  2210.  
  2211. #ifdef LINK_SPEC
  2212.   p = LINK_SPEC;
  2213.   while (c = *p++)
  2214.     if (c == '%' && *p == '{')
  2215.       /* We have a switch spec.  */
  2216.       validate_switches (p + 1);
  2217. #endif
  2218.  
  2219. #ifdef LIB_SPEC
  2220.   p = LIB_SPEC;
  2221.   while (c = *p++)
  2222.     if (c == '%' && *p == '{')
  2223.       /* We have a switch spec.  */
  2224.       validate_switches (p + 1);
  2225. #endif
  2226.  
  2227. #ifdef STARTFILE_SPEC
  2228.   p = STARTFILE_SPEC;
  2229.   while (c = *p++)
  2230.     if (c == '%' && *p == '{')
  2231.       /* We have a switch spec.  */
  2232.       validate_switches (p + 1);
  2233. #endif
  2234. }
  2235.  
  2236. /* Look at the switch-name that comes after START
  2237.    and mark as valid all supplied switches that match it.  */
  2238.  
  2239. void
  2240. validate_switches (start)
  2241.      char *start;
  2242. {
  2243.   register char *p = start;
  2244.   char *filter;
  2245.   register int i;
  2246.  
  2247.   if (*p == '|')
  2248.     ++p;
  2249.  
  2250.   if (*p == '!')
  2251.     ++p;
  2252.  
  2253.   filter = p;
  2254.   while (*p != ':' && *p != '}') p++;
  2255.  
  2256.   if (p[-1] == '*')
  2257.     {
  2258.       /* Mark all matching switches as valid.  */
  2259.       --p;
  2260.       for (i = 0; i < n_switches; i++)
  2261.     if (!strncmp (switches[i].part1, filter, p - filter))
  2262.       switches[i].valid = 1;
  2263.     }
  2264.   else
  2265.     {
  2266.       /* Mark an exact matching switch as valid.  */
  2267.       for (i = 0; i < n_switches; i++)
  2268.     {
  2269.       if (!strncmp (switches[i].part1, filter, p - filter)
  2270.           && switches[i].part1[p - filter] == 0)
  2271.         switches[i].valid = 1;
  2272.     }
  2273.     }
  2274. }
  2275.